/***
 * @Author:xxx
 * @Date:2020-08-26 10:54:02
 * @LastModifiedBy:xxx
 * @Last Modified time:2020-08-26 11:00:25
 */

import axios from 'axios';
import { message } from 'antd';
import { getToken } from '../utils/cookies';

export function ajax(url,data={},type='GET') {
  const data1 = JSON.stringify(data);
  return new Promise((resolve) => {
    let promise;
    axios.defaults.headers.post['Content-Type'] = 'application/json';
    axios.defaults.headers.common['Authorization'] = getToken();
    if(type==='GET') {
      promise = axios.get(url,{
        params: {data}  
      }
      );
    }else{
      promise = axios.post(url,data1);
    }

    promise.then((request) => {  // 请求成功
      resolve(request.data);
    }).catch((error) => {  // 请求失败
      if(error.message.indexOf('403') !== -1){
        message.error("无访问权限！");
      }else{
        message.error('请求失败：'+ error.message);
      }
    })
  })   
};


export function noToken(url,data={},type='GET') {
  const data1 = JSON.stringify(data);
  return new Promise((resolve) => {
    let promise;
    axios.defaults.headers.post['Content-Type'] = 'application/json';
    if(type==='GET') {
      promise = axios.get(url,{
        params: {data}  
      });
    }else{
      promise = axios.post(url,data1);
    }

    promise.then((request) => {  // 请求成功
      resolve(request.data);
    }).catch((error) => {  // 请求失败
      message.error('请求失败：'+ error.message);
    })
  })   
};





